package com.yimi.yinhepda.views.offline.load;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.pda.ym.base.BaseActivity;
import com.yimi.yinhepda.R;
import com.yimi.yinhepda.adapter.OffLineScanAdapter;
import com.pda.ym.callbacks.UICallBack;
import com.yimi.yinhepda.db.dao.LoadUnloadGroupTableDao;
import com.yimi.yinhepda.db.dao.ScanDetailVoDao;
import com.yimi.yinhepda.db.dao.UnloadScanTableDao;
import com.yimi.yinhepda.db.dao.UnloadTaskTableDao;
import com.yimi.yinhepda.db.table.LoadUnLoadGroupTable;
import com.yimi.yinhepda.db.table.ScanDetailVo;
import com.pda.ym.db.table.UnloadScanTable;
import com.yimi.yinhepda.db.table.UnloadTaskTable;
import com.yimi.yinhepda.entity.load_unload_offline_entity.RequestBarEntity;
import com.yimi.yinhepda.entity.response.OfflineLoadResponse;
import com.pda.ym.net.NetRequest;
import com.yimi.yinhepda.utils.Common;
import com.pda.ym.utils.Constants;
import com.pda.ym.utils.DateHelper;
import com.pda.uce.commom.util.LogUtil;
import com.pda.uce.commom.util.MyDialog;
import com.pda.ym.utils.SoundVibratorManager;
import com.pda.ym.utils.StringUtils;
import com.yimi.yinhepda.views.createPackage_module.CreateDeptSelectActivity;
import com.yimi.yinhepda.views.load_module.LoadSuccessActivity;
import com.yimi.yinhepda.views.load_module.LoadUnloadGroupActivity;
import com.yimi.yinhepda.views.unload_module.UnloadCompareActivity;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 离线装车，扫描界面
 *
 * @author 000432
 */
public class OfflLneLoadActivity extends BaseActivity {
    private TextView num, totlePiao, totleJian;
    private UnloadScanTableDao mUnloadScanTableDao;
    private ScanDetailVoDao scanDetailVoDao;
    private UnloadTaskTableDao taskTableDao;
    private TextView nextStation;
    private ListView listview;
    private String mDeptCode, mDeptName, lineType, offlineTaskNo, toCompany;
    private List<ScanDetailVo> scanList;
    private int operTypeCode = 3;
    private OffLineScanAdapter scanAdapter;
    /**
     * 配载单对象
     */
    private UnloadTaskTable taskTable;
    private String userCode;
    /**
     * 本地任务序号
     */
    private Integer order;
    /**
     * 月台号
     */
    private String platformNo;
    /**
     * 参与的装卸人工号
     */
    private String joinWrokNum;
    /**
     * 参与的装卸人姓名
     */
    private String joinUserName;
    /**
     * 修改装卸人
     */
    private Button btnModifyGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_offline_load);
        initView();
        initData();
    }

    public OfflLneLoadActivity() {
        super(true);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //设置未上传数
        num.setText((int) mUnloadScanTableDao.queryCount("isUpload", 0, operTypeCode, userCode) + "");
        //获取装卸组信息
        getGroupInfo();
    }

    @Override
    public String getPageName() {
        return "离线装车";
    }

    private void initView() {
        ((TextView) findViewById(R.id.title)).setText("离线装车");
        num =  findViewById(R.id.num);
        nextStation =  findViewById(R.id.next_station_ed);
        nextStation.setOnClickListener(this);
        findViewById(R.id.load_offline_back).setOnClickListener(this);
        findViewById(R.id.load_offline_upload).setOnClickListener(this);
        // 手动添加
        findViewById(R.id.btn_add).setOnClickListener(this);
        // 撤销装车
        findViewById(R.id.btn_del).setOnClickListener(this);

        totlePiao =  findViewById(R.id.totle_piao);
        totleJian =  findViewById(R.id.totle_jian);
        listview =  findViewById(R.id.load_offline_listview);
        //修改装卸人按钮
        btnModifyGroup =  findViewById(R.id.btn_modifyLoadUnloadPeople);
        btnModifyGroup.setOnClickListener(this);

        userCode = sharedPreferUtil.getValue(Common.USER_CODE);
        mUnloadScanTableDao = new UnloadScanTableDao();
        scanDetailVoDao = new ScanDetailVoDao();
        taskTableDao = new UnloadTaskTableDao();
    }

    private void initData() {
        //获取装卸组信息
        getGroupInfo();
        Bundle bundle = getIntent().getExtras();
        //获取下一站
        mDeptCode = bundle.getString("deptCode");
        mDeptName = bundle.getString("deptName");
        lineType = bundle.getString("lineType");
        toCompany = bundle.getString("toCompany");
        nextStation.setText(mDeptName);

        scanList = new ArrayList<>();
        //上个界面的离线任务号
        if (offlineTaskNo == null) {
            offlineTaskNo = bundle.getString("offlineTaskNo");
        }
        if (offlineTaskNo != null) {
            //列表数据
            scanList = scanDetailVoDao.getDataByStowageAndType(offlineTaskNo, operTypeCode, userCode);
            if (scanList == null) {
                scanList = new ArrayList<>();
            }
            //配载单
            List<UnloadTaskTable> stowage = taskTableDao.findStowage(offlineTaskNo, userCode);
            if (stowage != null && !stowage.isEmpty()) {
                taskTable = stowage.get(0);
                mDeptCode = taskTable.getDestDeptCode();
                mDeptName = taskTable.getDestDeptName();
                lineType = taskTable.getBizTypeCode().toString();
                toCompany = taskTable.getToCompCode();
            }
        }

        //设置列表数据
        if(scanAdapter == null){
            scanAdapter = new OffLineScanAdapter(this, scanList);
            listview.setAdapter(scanAdapter);
        }else{
            scanAdapter.setDataList(scanList);
            scanAdapter.notifyDataSetChanged();
        }
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                Intent intent = new Intent(OfflLneLoadActivity.this, UnloadCompareActivity.class);
                Bundle bundle = new Bundle();
                String waybillNo = scanList.get(position).getWaybillNo();
                bundle.putString("offlineTaskNo", offlineTaskNo);
                bundle.putString("userCode", userCode);
                bundle.putString("waybillNo", waybillNo);
                bundle.putInt("operTypeCode", operTypeCode);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        //设置总票，总件数
        if (taskTable != null) {
            totlePiao.setText(String.valueOf(taskTable.getPriceNum()));
            totleJian.setText(String.valueOf(taskTable.getPiecesNum()));
            nextStation.setText(taskTable.getDestDeptName());
        }
    }

    /**
     * 获取装卸组信息
     */
    private void getGroupInfo() {
        //获取登录的部门类型
        int deptType = sharedPreferUtil.getIntValue(Common.DEPT_TYPE);
        if (deptType == 5 || deptType == 6) {
            LoadUnLoadGroupTable loadUnLoadGroupTable = new LoadUnloadGroupTableDao().getData();
            if (loadUnLoadGroupTable != null) {
                platformNo = loadUnLoadGroupTable.getPlatform();
            }
            //登录分拨获取装卸组
            joinWrokNum = getIntent().getStringExtra("joinWrokNum");
            joinUserName = getIntent().getStringExtra("joinUserName");
            if ((joinWrokNum == null || joinWrokNum.equals("")) && ((joinUserName == null) || joinUserName.equals(""))) {
                joinWrokNum = sharedPreferUtil.getValue(Common.USER_CODE);
                joinUserName = sharedPreferUtil.getValue(Common.USER_NAME);
                if (joinUserName == null || joinUserName.equals("")) {
                    joinUserName = joinWrokNum;
                }
            } else if (joinUserName == null || joinUserName.equals("")) {
                joinUserName = joinWrokNum;
            }
            btnModifyGroup.setVisibility(View.VISIBLE);
        } else {
            joinWrokNum = sharedPreferUtil.getValue(Common.USER_CODE);
            joinUserName = sharedPreferUtil.getValue(Common.USER_NAME);
            if (joinUserName == null || joinUserName.equals("")) {
                joinUserName = joinWrokNum;
            }
            btnModifyGroup.setVisibility(View.GONE);
        }

        //自动检测没有省区的工号，自动添加上去
        String compCode = sharedPreferUtil.getCompanyCode();
        if(compCode == null || "".equals(compCode)){
            showToast("企业编码为空！");
        }
        String[] userCodes = joinWrokNum.split(",");
        for (int i = 0; i < userCodes.length; i++) {
            String userCode = userCodes[i];
            if (!userCode.contains("@")) {
                userCodes[i] = userCode + "@" + compCode;
            }
        }
        StringBuilder tempUserCodes = new StringBuilder();
        for (String userCode : userCodes) {
            tempUserCodes.append(userCode + ",");
        }
        joinWrokNum = tempUserCodes.substring(0, tempUserCodes.length() - 1);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_add:
                if ("".equals(nextStation.getText().toString().trim())) {
                    Toast.makeText(OfflLneLoadActivity.this, "请先选择下一站", Toast.LENGTH_SHORT).show();
                    return;
                }

                //获取装卸组信息
                getGroupInfo();

                Intent unloadScanIntent = new Intent(OfflLneLoadActivity.this, OffLineAddActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("offlineTaskNo", offlineTaskNo);
                bundle.putString("mDeptCode", mDeptCode);
                bundle.putString("mDeptName", mDeptName);
                bundle.putString("lineType", lineType);
                bundle.putString("toCompany", toCompany);
                bundle.putString("platformNo", platformNo);
                bundle.putString("joinUserName", joinUserName);
                bundle.putString("joinWrokNum", joinWrokNum);
                unloadScanIntent.putExtras(bundle);
                startActivityForResult(unloadScanIntent, 1);
                break;
            case R.id.btn_del:
                if (scanList == null) {
                    Toast.makeText(OfflLneLoadActivity.this, "该配载单未扫描数据", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent deleteScanIntent = new Intent(OfflLneLoadActivity.this, OffLineDeleteActivity.class);
                Bundle deleteBundle = new Bundle();
                deleteBundle.putString("offlineTaskNo", offlineTaskNo);
                deleteScanIntent.putExtras(deleteBundle);
                startActivityForResult(deleteScanIntent, 2);
                break;
            case R.id.load_offline_back:
                finish();
                break;
            case R.id.load_offline_upload:
                //提交
                submitMethod();
                break;
            case R.id.next_station_ed:
                if (scanList != null && !scanList.isEmpty()) {
                    showToast("已扫描运单后不可选择下一站！");
                    return;
                }
                Intent intent = new Intent(OfflLneLoadActivity.this, CreateDeptSelectActivity.class);
                intent.putExtra("currentDept", sharedPreferUtil.getDeptCode());
                intent.putExtra("inputType", 1);
                startActivityForResult(intent, 0);
                break;
            case R.id.btn_modifyLoadUnloadPeople:
                intent = new Intent(this, LoadUnloadGroupActivity.class);
                bundle = new Bundle();
                //扫描类型：1-装车扫描，2-卸车扫描,3-离线装车扫描，4-离线卸车扫描
                bundle.putInt("scanType", 3);
                //装车类型：1-装车，2-卸车,3-离线装车，4-离线卸车
                bundle.putInt("loadType", 3);
                //下一站
                bundle.putString("deptCode", mDeptCode);
                bundle.putString("deptName", mDeptName);
                bundle.putString("lineType", lineType);
                bundle.putString("toCompany", toCompany);
                //离线任务号
                bundle.putString("offlineTaskNo", offlineTaskNo);

                intent.putExtras(bundle);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == 0) {
            mDeptCode = data.getStringExtra("deptCode");
            mDeptName = data.getStringExtra("deptName");
            lineType = data.getStringExtra("lineType");
            toCompany = data.getStringExtra("toCompany");
            nextStation.setText(mDeptName);
        } else if (resultCode == RESULT_OK && (requestCode == 1 || requestCode == 2)) {
            offlineTaskNo = data.getStringExtra("offlineTaskNo");
            initData();
        }
    }

    @Override
    protected void handleBarCode(String result) {
        //关闭扫描
        setScannerEnabled(false);
        if ("".equals(nextStation.getText().toString())) {
            Toast.makeText(OfflLneLoadActivity.this, "请先选择网点", Toast.LENGTH_SHORT).show();
            //开启扫描
            setScannerEnabled(true);
            SoundVibratorManager.playSound(2);
            return;
        }
        if (!StringUtils.isWaybillNo(result)) {
            Toast.makeText(OfflLneLoadActivity.this, "运单号错误", Toast.LENGTH_SHORT).show();
            //开启扫描
            setScannerEnabled(true);
            SoundVibratorManager.playSound(2);
            return;
        }

        //处理扫描
        boolean isInclude = false;
        if (scanList != null && !scanList.isEmpty()) {
            for (int i = 0; i < scanList.size(); i++) {
                //如果在列表中
                if (scanList.get(i).getWaybillNo().equals(result.substring(0, 12))) {
                    //通任务的子单号
                    List<UnloadScanTable> childWaybillNo = mUnloadScanTableDao.getChildWaybillNo(offlineTaskNo, operTypeCode, userCode, result);
                    if (childWaybillNo != null && !childWaybillNo.isEmpty()) {
                        Toast.makeText(OfflLneLoadActivity.this, "运单号重复扫描", Toast.LENGTH_SHORT).show();
                        //开启扫描
                        setScannerEnabled(true);
                        SoundVibratorManager.playSound(2);
                        return;
                    }
                    //更新列表内容
                    ScanDetailVo scanDetailVo = scanList.get(i);
                    scanDetailVo.setQuantity(scanDetailVo.getQuantity() + 1);
                    scanDetailVo.setScanTime(DateHelper.getDateFormateOffline());
                    scanList.remove(i);
                    scanList.add(0, scanDetailVo);
                    //更新列表
                    scanAdapter.notifyDataSetChanged();
                    //保存运单
                    UnloadScanTable unloadScanTable = saveWaybill(result, 1);
                    //更新配载单件数
                    taskTable.setPiecesNum(taskTable.getPiecesNum() + 1);
                    try {
                        scanDetailVoDao.update(scanDetailVo);
                        taskTableDao.update(taskTable);
                        mUnloadScanTableDao.insert(unloadScanTable);
                    } catch (SQLException e) {
                        LogUtil.e(e.toString());
                        LogUtil.e(e.toString());
                    }
                    isInclude = true;
                    break;
                }
            }
        } else {
//            int count = (int) mUnloadScanTableDao.queryCount("isUpload", 0, operTypeCode, userCode);
//            if (count > 9) {
//                Toast.makeText(OfflLneLoadActivity.this, "你的离线任务很多啊，兄弟", Toast.LENGTH_SHORT).show();
//                //开启扫描
//                setScannerEnabled(true);
//                SoundVibratorManager.playSound(2);
//                return;
//            }

            //同样始发站-目的站-日期条件下，的order
            if (order == null || order == 0) {
                order = taskTableDao.findByTime(DateHelper.getDateFormateShort(new Date()),
                        sharedPreferUtil.getDeptCode(), mDeptCode, userCode);
                order = order == null ? 1 : order + 1;
            }
            if (offlineTaskNo == null || offlineTaskNo.equals("")) {
                offlineTaskNo = "Z" + DateHelper.getDateTimeFormateForSimple4(new Date()) + mDeptCode + order;
            }
            boolean isNull = false;
            if (taskTable == null) {
                isNull = true;
                //创建配载单
                taskTable = new UnloadTaskTable();
            }
            taskTable.setCreateTime(Constants.getCurrentTimeFormat1());
            taskTable.setShowTime(DateHelper.getDateFormateShort(new Date()));
            //目的站code
            taskTable.setDestDeptCode(mDeptCode);
            //目的站名称
            taskTable.setDestDeptName(mDeptName);
            //始发站code
            taskTable.setSrcDeptCode(sharedPreferUtil.getDeptCode());
            //始发站名称
            taskTable.setSrcDeptName(sharedPreferUtil.getValue(Common.DEPT_NAME));
            // 序号
            taskTable.setOrder(order);
            //操作员工
            taskTable.setOperEmpCode(userCode);
            //干线，支线
            taskTable.setBizTypeCode(Integer.parseInt(lineType));
            //离线号
            taskTable.setOfflineTaskNo(offlineTaskNo);
            //件数
            taskTable.setPiecesNum(1);
            //票数
            taskTable.setPriceNum(1);
            taskTable.setToCompCode(toCompany);
            taskTable.setStatus(0);
            try {
                if (isNull) {
                    taskTableDao.insert(taskTable);
                } else {
                    taskTableDao.update(taskTable);
                }
            } catch (SQLException e) {
                LogUtil.e(e.toString());
            }
        }
        //主单不在列表中
        if (!isInclude) {
            ScanDetailVo scanDetail = new ScanDetailVo();
            scanDetail.setWaybillNo(result.substring(0, 12));
            scanDetail.setInputType(1);
            scanDetail.setQuantity(1);
            scanDetail.setScanTime(DateHelper.getDateFormateOffline());
            scanDetail.setCreateTime(Constants.getCurrentTimeFormat1());
            scanDetail.setOfflineTaskNo(offlineTaskNo);
            scanDetail.setOperTypeCode(operTypeCode);
            scanDetail.setOperEmpCode(userCode);
            //更新列表
            scanList.add(0, scanDetail);
            scanAdapter.notifyDataSetChanged();
            //保存运单
            UnloadScanTable unloadScanTable = saveWaybill(result, 1);
            try {
                //不在列表中，更新件数（但是首次的时候，不走这个逻辑）
                if (scanList.size() > 1) {
                    taskTable.setPiecesNum(taskTable.getPiecesNum() + 1);
                    taskTable.setPriceNum(taskTable.getPriceNum() + 1);
                    taskTableDao.update(taskTable);
                }
                scanDetailVoDao.insert(scanDetail);
                mUnloadScanTableDao.insert(unloadScanTable);
            } catch (SQLException e) {
                LogUtil.e(e.toString());
            }
        }

        //设置总票，总件数
        if (taskTable != null) {
            totlePiao.setText(String.valueOf(taskTable.getPriceNum()));
            totleJian.setText(String.valueOf(taskTable.getPiecesNum()));
        }
        //设置未上传数
        num.setText((int) mUnloadScanTableDao.queryCount("isUpload", 0, operTypeCode, userCode) + "");
        //开启扫描
        setScannerEnabled(true);
        SoundVibratorManager.playSound(1);
    }

    /**
     * 保存运单
     *
     * @param result 扫描到的子单号
     * @param type   输入类型；手动或扫描
     * @return 运单实体
     */
    private UnloadScanTable saveWaybill(String result, int type) {

        UnloadScanTable unloadScan = new UnloadScanTable();
        //时间
        unloadScan.setScanTime(Constants.getCurrentTimeFormat1());
        unloadScan.setOperTime(Constants.GetSysTime());
        // 子单
        unloadScan.setChildWaybillNo(result);
        // 主单
        unloadScan.setWaybillNo(result.substring(0, 12));
        // 扫描类型
        unloadScan.setInputType(type);
        // 操作类型
        unloadScan.setOperTypeCode(operTypeCode);
        // 配载单号
        unloadScan.setOfflineTaskNo(offlineTaskNo);
        unloadScan.setOperEmpCode(userCode);
        //设置月台号
        unloadScan.setPlatformNo(platformNo);
        //装卸组参与人
        unloadScan.setOperPersonNames(joinUserName);
        unloadScan.setOperPersonCodes(joinWrokNum);
        return unloadScan;
    }

    /**
     * 上传扫描的数据
     */
    private void upload() {
        showPgDlg("正在提交离线装车数据...");
        //关闭扫描
        setScannerEnabled(false);

        final List<UnloadTaskTable> unloadTasks = taskTableDao.findStowage(offlineTaskNo, userCode);
        final List<UnloadScanTable> unloadDatas = mUnloadScanTableDao.getDataByStowageAndType(offlineTaskNo, operTypeCode, userCode);
        final List<ScanDetailVo> unloadScans = scanDetailVoDao.getDataByStowageAndType(offlineTaskNo, operTypeCode, userCode);

        if (unloadDatas != null && !unloadDatas.isEmpty()) {
            //实例化上传类
            RequestBarEntity uploadRequest = new RequestBarEntity();
            //本地配载单
            uploadRequest.setStowageNo(offlineTaskNo);
            // 始发网点
            uploadRequest.setSrcDeptCode(sharedPreferUtil.getDeptCode());
            // 始发站名称
            uploadRequest.setSrcDeptName(sharedPreferUtil.getValue(Common.DEPT_NAME));
            // 目的网店code
            uploadRequest.setDestDeptCode(mDeptCode);
            //目的网点名称
            uploadRequest.setDestDeptName(mDeptName);
            //路由
            uploadRequest.setRoute(sharedPreferUtil.getDeptCode() + "-" + mDeptCode);
            //员工code
            uploadRequest.setOperEmpCode(userCode);
            // 员工名称
            uploadRequest.setOperEmpName(sharedPreferUtil.getEmpName());
            //上传时间
            uploadRequest.setScanTime(Constants.getCurrentTimeFormat1());
            //公司编码
            uploadRequest.setCompCode(sharedPreferUtil.getCompanyCode());
            uploadRequest.setToCompCode(toCompany);
            //线路类型
            uploadRequest.setBizTypeCode(Integer.parseInt(lineType));
            //运单数据
            uploadRequest.setRecords(unloadDatas);
            //开始时间
            uploadRequest.setStartTime(mUnloadScanTableDao.getScanEndTime(offlineTaskNo, operTypeCode, userCode, true));
            //结束时间
            uploadRequest.setEndTime(mUnloadScanTableDao.getScanEndTime(offlineTaskNo, operTypeCode, userCode, false));

            new NetRequest().setMethod(NetRequest.Method.POST_STRING)
                    .setResponsClazz(OfflineLoadResponse.class)
                    .setUrl(Constants.OFFLINE_UPLOAD_LOAD)
                    .setRequestObject(uploadRequest)
                    .execute(new UICallBack() {
                        @Override
                        public void onError(String msg) {
                            //显示错误信息
                            dismissPgDlg();
                            //开启扫描
                            setScannerEnabled(true);
                            Log.e("onError", "msg = " + msg);
                            Toast.makeText(OfflLneLoadActivity.this, "提交失败:" + msg, Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onSuccess(Object obj) {
                            OfflineLoadResponse response = (OfflineLoadResponse) obj;
                            //更新上传的数据
                            try {
                                unloadTasks.get(0).setStatus(1);
                                taskTableDao.update(unloadTasks.get(0));
                            } catch (SQLException e) {
                                LogUtil.e(e.toString());
                            }
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        scanDetailVoDao.delete(unloadScans);
                                        mUnloadScanTableDao.delete(unloadDatas);
                                    } catch (SQLException e) {
                                        LogUtil.e(e.toString());
                                    }
                                }
                            }).start();
                            dismissPgDlg();
                            //开启扫描
                            setScannerEnabled(true);

                            //跳转界面
                            Intent unloadScanIntent = new Intent(OfflLneLoadActivity.this, LoadSuccessActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putInt("type", 1);
                            bundle.putString("stowageNo", response.getData());
                            bundle.putString("offlineTaskNo", offlineTaskNo);
                            bundle.putString("src", sharedPreferUtil.getValue(Common.DEPT_NAME));
                            bundle.putString("desc", mDeptName);
                            bundle.putString("carNo", unloadTasks.get(0).getPriceNum() + "");
                            bundle.putString("realWeight", unloadTasks.get(0).getPiecesNum() + "");
                            unloadScanIntent.putExtras(bundle);
                            startActivity(unloadScanIntent);

                            finish();
                        }
                    });
        }
    }

    /**
     * 提交数据,提交后，配载单不删除
     */
    private void submitMethod() {
        //校验是否有该配载单的数据
        if (scanList == null || scanList.isEmpty()) {
            showToast(getString(R.string.tmp_no_data));
            return;
        }
        MyDialog myDialog = new MyDialog(this);
        myDialog.setIcon(R.drawable.alert);
        myDialog.setMessage("是否完成装车提交任务？");
        myDialog.setPositiveClickListener( "是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                //跳转界面
                Intent unloadScanIntent = new Intent(OfflLneLoadActivity.this, OffLineLoadSuccessActivity.class);
                Bundle bundle = new Bundle();
                //离线装车类型：1-离线装车，2-离线卸车
                bundle.putInt("loadType", 1);
                bundle.putString("offlineTaskNo", offlineTaskNo);
                bundle.putString("srcDept", sharedPreferUtil.getValue(Common.DEPT_NAME));
                bundle.putString("destDept", mDeptName);
                bundle.putString("loadPiaoNum", totlePiao.getText().toString());
                bundle.putString("loadJianNum", totleJian.getText().toString());
                bundle.putString("lineType", lineType);
                unloadScanIntent.putExtras(bundle);
                startActivity(unloadScanIntent);
                finish();
            }
        });
        myDialog.setNegativeClickListener(R.drawable.btn_cancel_selector, "否", null);
        myDialog.show();
    }
}
